package cn.tedu.car.backend.user.controller;


import cn.tedu.car.backend.base.response.JsonResult;
import cn.tedu.car.backend.user.enums.UserStatus;
import cn.tedu.car.backend.user.pojo.dto.UserQueryParam;
import cn.tedu.car.backend.user.pojo.dto.UserSaveParam;
import cn.tedu.car.backend.user.pojo.vo.UserVO;
import cn.tedu.car.backend.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/listUser")
    public JsonResult listUser(UserQueryParam userQueryParam) {
        List<UserVO> userVOS = userService.listUser(userQueryParam);
        return JsonResult.ok(userVOS);
    }

    /**
     * 用户保存（新增或修改）
     * @param userSaveParam
     * @return
     */
    @PostMapping("/saveUser")
    public JsonResult saveUser(@Validated UserSaveParam userSaveParam) {
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @PostMapping("/{userId}/enable")
    public JsonResult enableUser(@PathVariable Long userId) {
        userService.changUserStatus(userId, UserStatus.DISABLE, UserStatus.NORMAL);
        return JsonResult.ok();
    }

    @PostMapping("/{userId}/disable")
    public JsonResult disableUser(@PathVariable Long userId) {
        userService.changUserStatus(userId, UserStatus.NORMAL, UserStatus.DISABLE);
        return JsonResult.ok();
    }

}
